#!/usr/bin/perl -w
#
# $Id: PowerBox.pm,v 1.16 2005/04/12 02:50:01 solovam Exp $
#
# This file is a part of gkismet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#
# PowerBox class
#
package PowerBox;

use Gtk;
use Misc;
use ReusableConnObserver;
use strict;
@PowerBox::ISA = qw(ReusableConnObserver);

my %maxValues = (cardpower => 256, cardnoise => 256);
my %minValues = (cardpower => 0, cardnoise => 0);

#
# Constructor
#
sub new
{
	my $type = shift;
	my $self = new ReusableConnObserver(@_);
	bless $self, $type;

	my $hbox = new Gtk::HBox($true, 0);
	for my $val (qw(cardpower cardnoise))
	{
		my $adjustment = new Gtk::Adjustment($minValues{$val}, $minValues{$val}, $maxValues{$val}, 0, 0, 0);
		my $bar = new_with_adjustment Gtk::ProgressBar($adjustment);
		$self->{$val . 'ProgressBar'} = $bar;
		$bar->set_orientation('bottom_to_top');
		$bar->set_show_text($false);
		$bar->set_format_string('  %v  ');
		$hbox->pack_start($bar, $true, $false, 5);
	}

	my $frame = new Gtk::Frame(undef);
	$frame->set_label('Card power');
	$frame->add($hbox);
	my $outerBox = new Gtk::HBox($false, 0);
	$self->{'outerBox'} = $outerBox;
	$outerBox->pack_start($frame, $true, $false, 0);
	$outerBox->show_all();

	return $self;
}

#
# Handle an update from an observable
#
sub update
{
	my $self = shift;
	my $object = shift;
	my $data = shift;

	if($object->isa('Connection') && defined $self->{'connection'} && $self->{'connection'} eq $object)
	{
		if($data->{'changed'} eq 'info')
		{
			for my $val (qw(cardpower cardnoise))
			{
				$self->{$val . 'ProgressBar'}->set_show_text($true);
				my $level = $self->{'connection'}->getInfo()->{$val};
				if($level < 0)
				{
					$level += 256;
				}
				my $prc = ($level - $minValues{$val}) / ($maxValues{$val} - $minValues{$val});
				if($prc > 1)
				{
					$prc = 1;
				}
				if($prc < 0)
				{
					$prc = 0;
				}
				$self->{$val . 'ProgressBar'}->update($prc);
			}
		}
	}
}

#
# Flush screen data
#
sub flush
{
	my $self = shift;
	for my $val (qw(cardpower cardnoise))
	{
		$self->{$val . 'ProgressBar'}->set_show_text($false);
		$self->{$val . 'ProgressBar'}->update(0);
	}
}

#
# Get Gtk widget
#
sub getWidget
{
	my $self = shift;
	return($self->{'outerBox'});
}

1;
